package com.tuoyu.peopletest.view.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


import com.tuoyu.peopletest.R;
import com.tuoyu.peopletest.scinterface.OnDownListener;
import com.tuoyu.peopletest.utils.SCLogUtils;
import com.tuoyu.peopletest.utils.SCUIUtils;

import androidx.annotation.Nullable;

/**
 * 小球
 */
public class MyRoundView extends View {
    private final static String TAG = "MyRoundView";
    //小球尺寸
    private int mRoundWidth = SCUIUtils.getDimen(R.dimen.sc_w_h_test_round);
    private float mRoundWidthHalf = mRoundWidth * 1f / 2;
    private final Paint mPaint;

    public MyRoundView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        int color = context.getResources().getColor(R.color.sc_c_test_red);
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.FILL);
    }

    public MyRoundView(Context context) {
        super(context);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        int color = context.getResources().getColor(R.color.sc_c_test_red);
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.FILL);
    }

    public void setColor(int color){
        mPaint.setColor(color);
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRoundWidth = getWidth();
       // SCLogUtils.e(TAG,"onSizeChanged getWidth="+getWidth());
       // SCLogUtils.e(TAG,"onSizeChanged mRoundWidth="+mRoundWidth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //SCLogUtils.e(TAG,"onDraw");
        canvas.drawCircle(mRoundWidthHalf,mRoundWidthHalf,mRoundWidthHalf,mPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getActionMasked()){
            case MotionEvent.ACTION_DOWN:
                SCLogUtils.e(TAG,"onTouchEvent ACTION_DOWN");
                setScaleX(1.1f);
                setScaleY(1.1f);
                onDownListener.onDown(this);
                break;
            case MotionEvent.ACTION_UP:
                setScaleX(1f);
                setScaleY(1f);
                SCLogUtils.e(TAG,"onTouchEvent ACTION_UP");
                onDownListener.onUp(this);
                break;
        }
        return true;
    }

    private OnDownListener onDownListener;
    public void setOnDownListener(OnDownListener onDownListener){
        this.onDownListener = onDownListener;
    }
}
